﻿//===========================[ 7 / 6 / 2011 LVI ]=============================
//				Copyright c Vitaly Lyaschenko <SkyChameleon@gmail.com>
//								Source file
// Purpose: 
//=============================================================================
#include "cwLogger.h"

std::vector<std::wstring> cwLogger::logString;
std::wstring	          cwLogger::logPath;

void cwLogger::create( const std::wstring& logfileName )
{   
    logPath = !logfileName.empty() ? logfileName : L"Log.txt";

    std::wfstream logFile( logPath, std::ios::out | std::ios::ate );
    logFile.close();
}

void cwLogger::create( const std::string &logfileName )
{   
    logPath = !logfileName.empty() ? to_wstring(logfileName) : L"Log.txt";

    std::wfstream logFile( logPath, std::ios::out | std::ios::ate );
    logFile.close();
}
// записать в лог
void cwLogger::writeFlush()
{   // открытие для добавления
    std::wfstream logFile( logPath, std::ios::out | std::ios::app );

    for (size_t siz = 0; siz < logString.size(); siz++)
        logFile << logString[siz];

    logString.clear();
    logFile.close();
}
// запись информации
void cwLogger::writeINFO( const std::wstring &info )
{  
    if (!info.empty())
    {
        logString.push_back(std::wstring(L" <+> ") + info + L"\n");
        writeFlush();
    }
}

void cwLogger::writeINFO( const std::string &info )
{
    if (!info.empty())
    {
        logString.push_back(std::wstring(L" <+> ") + to_wstring(info) + L"\n");
        writeFlush();
    }
}

// запись ошибки
void cwLogger::writeERROR( const std::wstring &info )
{   
    if (!info.empty())
    {
        logString.push_back(std::wstring(L" <!> ") + info + L"\n");
        writeFlush();
    }
}

void cwLogger::writeERROR( const std::string &info )
{   
    if (!info.empty())
    {
        logString.push_back(std::wstring(L" <!> ") + to_wstring(info) + L"\n");
        writeFlush();
    }
}
// запись критической ошибки
void cwLogger::writeFATAL_ERROR( const std::wstring &info )
{   
    if (!info.empty())
    {
        logString.push_back(std::wstring(L" <X> ") + info + L"\n");
        writeFlush();
    }
}

void cwLogger::writeFATAL_ERROR( const std::string &info )
{   
    if (!info.empty())
    {
        logString.push_back(std::wstring(L" <X> ") + to_wstring(info) + L"\n");
        writeFlush();
    }
}